package com.xsz.pda.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 信息重置计时间
 */
public class Resetor {


    public interface OnReset{
        void onReset();
    }
    private long resetDuration;
    private AtomicReference<TimerTask> resetUITask = new AtomicReference<>();
    private List<OnReset> onResetList = new ArrayList<>();

    public Resetor(long resetDuration) {
        this.resetDuration = resetDuration;
    }

    public void addOnResetListener(OnReset onReset){
        onResetList.add(onReset);
    }

    public void resetInfoDelay() {
        TimerTask task = resetUITask.get();
        if (task!=null){
            task.cancel();
        }
        task = new TimerTask() {
            @Override
            public void run() {
                for (OnReset onReset:onResetList) {
                    onReset.onReset();
                }
            }
        };
        new Timer().schedule(task, resetDuration);
        resetUITask.set(task);
    }

    public void stop(){
        TimerTask task = resetUITask.get();
        if (task!=null){
            task.cancel();
        }
    }
}
